# design_patterns.py

# 单例模式 (Singleton Pattern)
class Singleton:
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

# 工厂模式 (Factory Pattern)
class Product:
    def operation(self):
        pass

class ConcreteProductA(Product):
    def operation(self):
        return "Result of ConcreteProductA"

class ConcreteProductB(Product):
    def operation(self):
        return "Result of ConcreteProductB"

class Creator:
    def factory_method(self):
        pass

class ConcreteCreatorA(Creator):
    def factory_method(self):
        return ConcreteProductA()

class ConcreteCreatorB(Creator):
    def factory_method(self):
        return ConcreteProductB()

# 命令模式 (Command Pattern)
class Command:
    def execute(self):
        pass

class Light:
    def turn_on(self):
        print("Light is ON")

    def turn_off(self):
        print("Light is OFF")

class LightOnCommand(Command):
    def __init__(self, light):
        self.light = light

    def execute(self):
        self.light.turn_on()

class LightOffCommand(Command):
    def __init__(self, light):
        self.light = light

    def execute(self):
        self.light.turn_off()

class RemoteControl:
    def __init__(self):
        self.command = None

    def set_command(self, command):
        self.command = command

    def press_button(self):
        self.command.execute()

# 装饰者模式 (Decorator Pattern)
class Coffee:
    def cost(self):
        pass

class BasicCoffee(Coffee):
    def cost(self):
        return 5

class MilkDecorator(Coffee):
    def __init__(self, coffee):
        self._coffee = coffee

    def cost(self):
        return self._coffee.cost() + 1

class SugarDecorator(Coffee):
    def __init__(self, coffee):
        self._coffee = coffee

    def cost(self):
        return self._coffee.cost() + 0.5

# 测试各个设计模式
if __name__ == "__main__":
    # 测试单例模式
    singleton1 = Singleton()
    singleton2 = Singleton()
    print("Singleton Test:", singleton1 is singleton2)  # 输出: True

    # 测试工厂模式
    creator_a = ConcreteCreatorA()
    product_a = creator_a.factory_method()
    print("Factory Pattern:", product_a.operation())  # 输出: Result of ConcreteProductA

    creator_b = ConcreteCreatorB()
    product_b = creator_b.factory_method()
    print("Factory Pattern:", product_b.operation())  # 输出: Result of ConcreteProductB

    # 测试命令模式
    light = Light()
    light_on = LightOnCommand(light)
    light_off = LightOffCommand(light)

    remote = RemoteControl()

    remote.set_command(light_on)
    remote.press_button()  # 输出: Light is ON

    remote.set_command(light_off)
    remote.press_button()  # 输出: Light is OFF

    # 测试装饰者模式
    basic_coffee = BasicCoffee()
    print("Basic coffee cost:", basic_coffee.cost())  # 输出: 5

    milk_coffee = MilkDecorator(basic_coffee)
    print("Coffee with milk cost:", milk_coffee.cost())  # 输出: 6

    sugar_milk_coffee = SugarDecorator(milk_coffee)
    print("Coffee with milk and sugar cost:", sugar_milk_coffee.cost())  # 输出: 6.5
